home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / StateManager / reflective01.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  5.2 KB  |  153 lines

  1. //--------------------------------------------------------------------------------------
  2. //
  3. // Reflective Lighting Model
  4. // Copyright (c) Microsoft Corporation. All rights reserved.
  5. //
  6. //--------------------------------------------------------------------------------------
  7.  
  8.  
  9. //--------------------------------------------------------------------------------------
  10. // Scene Setup
  11. //--------------------------------------------------------------------------------------
  12.  
  13. // light direction (world space)
  14. float3 lightDir = {0.577, -0.577, -0.577};
  15.  
  16.  
  17. // light intensity
  18. float4 I_a = { 0.5f, 0.5f, 0.5f, 1.0f };    // ambient
  19. float4 I_d = { 0.5f, 0.5f, 0.5f, 1.0f };    // diffuse
  20. float4 I_s = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular
  21.  
  22. // Transformation Matrices
  23. matrix matView      : VIEW;
  24. matrix matProj      : PROJECTION;
  25. matrix matWorld     : WORLD;
  26.  
  27. //--------------------------------------------------------------------------------------
  28. // Material Properties
  29. //--------------------------------------------------------------------------------------
  30.  
  31. // Set by EffectInstance when mesh is loaded
  32. // (Default values provided for Effect Edit)
  33. float4 Diffuse = float4( 0.358824f, 0.311765f, 0.059804f, 1.f );
  34. float4 Ambient = float4( 0.358824f, 0.311765f, 0.059804f, 1.f );
  35. float4 Specular = float4( 0.9f, 0.9f, 0.9f, 0.f );
  36. float4 Emissive = float4( 0.f, 0.f, 0.f, 0.f );
  37. float  Power = 32.f;
  38. float  k_r = 0.20f;
  39.     
  40.  
  41. // Texture Parameters
  42. texture Texture0 < string name = "signs2.jpg"; >;
  43. texture Texture1 < string type = "CUBE"; string name = "skybox02.dds"; >;
  44.  
  45.  
  46. sampler linear_sampler = sampler_state
  47. {
  48.     Texture   = (Texture0);
  49.     MipFilter = LINEAR;
  50.     MinFilter = ANISOTROPIC;
  51.     MagFilter = LINEAR;
  52. };
  53.  
  54. sampler envmap_sampler = sampler_state
  55. {
  56.     Texture   = (Texture1);
  57.     MipFilter = LINEAR;
  58.     MinFilter = LINEAR;
  59.     MagFilter = LINEAR;
  60. };
  61.  
  62.  
  63. //--------------------------------------------------------------------------------------
  64. // Vertex Shader Output
  65. //--------------------------------------------------------------------------------------
  66. struct VS_OUTPUT
  67. {
  68.     float4 Pos  : POSITION;
  69.     float3 Diff : COLOR0;
  70.     float3 Spec : COLOR1;
  71.     float2 T0   : TEXCOORD0;
  72.     float3 T1   : TEXCOORD1;
  73.     float3 Reflect : TEXCOORD2;
  74. };
  75.  
  76.  
  77. //--------------------------------------------------------------------------------------
  78. // Vertex Shader
  79. //--------------------------------------------------------------------------------------
  80. VS_OUTPUT VS(
  81.     float3 Pos  : POSITION, 
  82.     float3 Norm : NORMAL,
  83.     float2 iT0  : TEXCOORD0 )
  84. {
  85.     VS_OUTPUT Out = (VS_OUTPUT)0;
  86.  
  87.     matrix matWorldView = mul( matWorld, matView );
  88.  
  89.     float3 L = -normalize(mul(lightDir,(float3x3)matView));
  90.  
  91.     float3 P = mul(float4(Pos, 1), (float4x3)matWorldView);      // position (view space)
  92.     float3 N = normalize(mul(Norm, (float3x3)matWorldView));     // normal (view space)
  93.  
  94.     float3 R = normalize(2 * dot(N, L) * N - L);             // Reflection vector (view space)
  95.     float3 V = -normalize(P);                                // view direction (view space)
  96.     float3 G = normalize(2 * dot(N, -P) * N + P);            // Glance vector (view space)
  97.     float  f = 0.5 - dot(V, N); f = 1 - 4 * f * f;           // fresnel term
  98.   
  99.  
  100.     Out.Pos   = mul(float4(P, 1), matProj);                          // position (projected)
  101.     Out.Diff  = I_a * Ambient + I_d * Diffuse * max(0, dot(N, L));   // diffuse + ambient
  102.     Out.Spec  = I_s * Specular * pow(max(0, dot(R, V)), Power/4);    // specular
  103.     Out.T0    = iT0;                                                 // Diffuse Tex coords
  104.     Out.T1    = mul( G, transpose( matView ) );                      // Glance Vector to View Space
  105.     Out.Reflect = max( 0, k_r * f);                                  // Reflection
  106.     
  107.     return Out;
  108. }
  109.  
  110.  
  111. //--------------------------------------------------------------------------------------
  112. // Pixel Shader
  113. //--------------------------------------------------------------------------------------
  114. float4 PS(
  115.     float3 Diff : COLOR0,
  116.     float3 Spec : COLOR1,
  117.     float2 Tex  : TEXCOORD0,
  118.     float3 EnvCoords : TEXCOORD1,
  119.     float  Reflect   : TEXCOORD2 ) : COLOR
  120. {
  121.     // Vertex Diffuse Lighting is modulated with the Diffuse Texture
  122.     float3 DiffuseC = Diff * tex2D( linear_sampler, Tex );
  123.     
  124.     // Sample the Environment map
  125.     float3 ReflectionC = Reflect * tex3D( envmap_sampler, EnvCoords );
  126.     
  127.     // Return the sum of Vertex-Specular, the Reflection, 
  128.     return float4( DiffuseC + ReflectionC + Spec, 1.f );
  129. }
  130.  
  131.  
  132. //--------------------------------------------------------------------------------------
  133. // Default Technique
  134. // Establishes Vertex and Pixel Shader
  135. // Ensures base states are set to required values
  136. // (Other techniques within the scene perturb these states)
  137. //--------------------------------------------------------------------------------------
  138. technique tec0
  139. {
  140.     pass P0
  141.     {
  142.         // shaders
  143.         VertexShader = compile vs_1_1 VS();
  144.         PixelShader  = compile ps_1_4 PS();
  145.  
  146.         ZEnable          = TRUE;
  147.         ZWriteEnable     = TRUE;
  148.         AlphaBlendEnable = FALSE;
  149.         CullMode         = CCW;
  150.         AlphaTestEnable  = FALSE;
  151.     }  
  152. }
  153.